<template>
  <!-- 物资采购结算-新增 -->
  <div class="e-form">
    <div class="tabs">
      <div class="tabs-title">基本信息</div>
      <div style="width: 100%" class="form">
        <el-form :model="formData" label-width="150px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="结算编号:">
                <el-input disabled v-model="formData.billNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单据机构:">
                <el-input
                  disabled
                  placeholder="请填写"
                  v-model="formData.orgName"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="结算类型:">
                <el-input
                  placeholder="请填写"
                  v-model="formData.settlementType"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="源单编号:">
                <el-input
                  placeholder="请填写"
                  v-model="formData.sourceBillNo"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="供应商:">
                <el-input
                  placeholder="请填写"
                  v-model="formData.supplierName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结算日期">
                <el-date-picker
                  v-model="formData.settlementDate"
                  type="date"
                  placeholder="选择日期"
                  :clearable="false"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="结算金额（元）">
                <el-input
                  placeholder="请填写"
                  v-model="formData.amount"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="验收人员">
                <el-input
                  disabled
                  placeholder="请填写"
                  v-model="formData.acceptancePersonnelName"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="发票编号:">
                <el-input
                  placeholder="请填写"
                  v-model="formData.invoiceNo"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="运输单位费用（含税，元）：">
                   <el-input
                  placeholder="请填写"
                  v-model="formData.freight"
                ></el-input>

                   </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="税率%">
                <el-input
                  placeholder="请填写"
                  v-model="formData.taxRate"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="税价合计">
                <el-input
                  placeholder="请填写"
                  v-model="formData.totalAmount"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="税额">
                <el-input
                  placeholder="请填写"
                  v-model="formData.taxAmount"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="税额差额">
                <el-input
                  placeholder="请填写"
                  v-model="formData.taxDifference"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="冲预付款金额（元）：">
                   <el-col :span="2">
                <el-checkbox
                  v-model="val"
                ></el-checkbox>
                </el-col>
                <el-col :span="22">
                 <el-input
                 v-if="val"
                  placeholder="请填写"
                  v-model="formData.offsetAdvancePayment"
                ></el-input>
                 </el-col>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单据来源：">
                <el-input
                  placeholder="请填写"
                  v-model="formData.source"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="冲个人借支：">
                <el-checkbox
                  v-model="formData.isPersonalBorrowing"
                  label=""
                ></el-checkbox>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="验收编号:">
                <el-input
                  placeholder="请填写"
                  v-model="formData.checkNo"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="冲各人借支金额（元）：">
                <el-input
                  placeholder="请填写"
                  v-model="formData.personalBorrowing"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="终期结算:">
                <el-checkbox
                  v-model="formData.telocinesiaClose"
                  label=""
                ></el-checkbox>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="冲销对象：">
                <el-input
                  placeholder="请填写"
                  v-model="formData.reversalObjectNumber"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="币种：">
                <el-input
                  placeholder="请填写"
                  v-model="formData.currency"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="借支人员：">
                <el-input
                  placeholder="请填写"
                  v-model="formData.borrowerName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="本位币：">
                <el-input
                  placeholder="请填写"
                  v-model="formData.baseCurName"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="传财务共享:">
                <el-input
                  placeholder="请填写"
                  v-model="formData.showEas"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结算金额（本位币）：">
                <el-input
                  placeholder="请填写"
                  v-model="formData.baseCurAmount"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="备注">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="formData.remarks"
              >
              </el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="状态">
              {{ formData.state }}
              <!-- 草稿/审核中/已审核/已关闭/已作废 -->
            </el-form-item>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="buttons">
      <el-button type="primary" style="background: #2e61d7" @click="save()"
        >保存</el-button
      >
      <el-button type="primary" style="background: #2e61d7" @click="submit()"
        >保存并提交</el-button
      >

      <el-button @click="cancel()">关闭</el-button>
    </div>
    <span style="display: none">{{ localPopConfirm }}</span>
  </div>
</template>

<script>
import '@/utils/jquery.scrollTo.min'
import { mapMutations, mapState } from 'vuex'
import { showSuccessPop } from '@/utils/common'

export default {
    data () {
        return {
            //表单数据
            formData: {
                billNo: '', //结算编号
                orgName: '', //单据机构
                settlementType: '', //结算类型：
                sourceBillNo: '', //源单编号
                supplierName: '', //供应商
                settlementDate: '', //结算日期
                amount: '', //结算金额（元）
                acceptancePersonnelName: '', //验收人员
                invoiceNo: '', //发票编号
                freight: '', //运输单位费用（含税，元）
                taxRate: '', //税率%：
                totalAmount: '', //税价合计
                taxAmount: '', //税额
                taxDifference: '', //税额差额
                offsetAdvancePayment: '', //冲预付款金额（元）：
                source: '', //单据来源
                isPersonalBorrowing: '', //冲个人借支
                checkNo: '', //验收编号
                personalBorrowing: '', //冲各人借支金额（元）：
                telocinesiaClose: '', //终期结算
                reversalObjectNumber: '', // /冲销对象
                currency: '', // /币种
                borrowerName: '', //借支人员
                baseCurName: '', //本位币
                showEas: '', //传财务共享
                baseCurAmount: '', //结算金额（本位币）
                remarks: '', //备注
                state: '草稿', //状态
            },
            orgId: '', //机构id
            orgName: '', //机构名称
            isSkip: false, //是否跳转
            val: '', //冲预付款金额（元）：
        }
    },
    computed: {
        ...mapState({
            options: state => state.contract.ctClassify,
            popConfirm: 'popConfirm',
            popStatus: 'popStatus',
        }),
        localPopConfirm () {
            if (this.popConfirm) {
                switch (this.popStatus) {
                case 'prompt':
                    this.delData()
                    break
                case 'success':
                    if (this.isSkip) this.handleClose()
                    break
                }
                this.setPopConfirm(false)
            }
            return this.popConfirm
        },
    },
    components: {},
    created () {
        let data = JSON.parse(localStorage.getItem('vuex'))
        this.orgId = data.userInfo.orgInfo.orgId
        this.orgName = data.userInfo.orgInfo.orgName
        this.formData.orgName = data.userInfo.orgInfo.orgName
        this.formData.acceptancePersonnelName = localStorage.getItem('userName')

    },
    mounted () {},
    beforeDestroy () {},
    methods: {
        ...mapMutations(['setPopConfirm']),

        //提交
        submit () {
            let settlementDate = this.getTime(this.formData.settlementDate)
            this.$Ajax
                .httpPost({
                    url: '/materialsettle/purchaseSettlement/create',
                    params: {
                        acceptancePersonnelId: '', //	验收人员id
                        acceptancePersonnelName: this.formData.acceptancePersonnelName, //	验收人员名称
                        amount: parseInt(this.formData.amount), //	结算金额(元)
                        baseCurAmount: parseInt(this.formData.baseCurAmount), //	结算金额(本位币)
                        baseCurId: '', //	本位币id
                        baseCurName: this.formData.baseCurName, //本位币名称
                        baseCurRate: 0, //本位币汇率
                        baseCurTaxAmount: 0, //	税额(本位币)
                        baseCurTotalAmount: 0, //	税价合计(本位币)
                        billId: '', //	物资采购结算id
                        billNo: '', //	结算编号
                        borrowerId: '', //	借资人员id
                        borrowerName: this.formData.borrowerName, //	借资人员名称
                        easId: '', //	财务Id
                        founderId: '', //	创建人Id
                        founderName: '', //创建人名称
                        freight: parseInt(this.formData.freight), //	运输单位费用(含税,元)
                        freightDtls: [], //物资采购结算-运费明细集合
                        gmtCreate: '', //	创建时间
                        gmtModified: '', //更新时间
                        invoiceNo: this.formData.invoiceNo, //发票编号
                        isOffsetAdvancePayment: 0, //	是否冲预付款
                        isPersonalBorrowing: parseInt(this.formData.isPersonalBorrowing), //是否冲个人借资
                        materialDtls: [], //物资采购结算-物资明细集合
                        nullifyCreated: '', //	作废时间
                        nullifyCreator: '', //	作废人
                        nullifyCreatorId: '', //	作废人id
                        nullifyDescription: '', //	作废描述
                        nullifyReason: '', //	作废原因
                        offsetAdvancePayment: parseInt(this.formData.offsetAdvancePayment), //	冲预付款金额(元)
                        orgId: this.orgId, //	所属机构id
                        orgName: this.formData.orgName, //	所属机构名称
                        personalBorrowing: parseInt(this.formData.personalBorrowing), //	冲个人借资(元)
                        purchasingUnitId: '', //	采购单位id
                        purchasingUnitName: '', //	采购单位名称
                        remarks: this.formData.remarks, //备注信息
                        returnDepositDtls: [], //物资采购结算-保证及明细集合
                        reversalObjectId: '', //	冲销对象id
                        reversalObjectNumber: this.formData.reversalObjectNumber, //	冲销对象编号
                        rmbAmount: 0, //	结算金额(人民币
                        rmbId: '', //	人民币id
                        rmbName: '', //人民币名称
                        rmbRate: 0, //	人民币汇率
                        rmbTaxAmount: 0, //	税额(人民币)
                        rmbTotalAmount: 0, //	税价合计(人民币)
                        settlementDate: settlementDate, //	结算日期
                        settlementType: parseInt(this.formData.settlementType), //	结算类型
                        sourceBillId: '', //	源单id
                        sourceBillNo: this.formData.sourceBillNo, //	源单编号
                        state: '0', //	单据状态
                        supplierId: '', //	供应商id
                        supplierName: this.formData.supplierName, //	供应商名称
                        taxAmount: parseInt(this.formData.taxAmount), //	税额
                        taxDifference: parseInt(this.formData.taxDifference), //	税额差额
                        taxRate: parseInt(this.formData.taxRate), //税率
                        totalAmount: parseInt(this.formData.totalAmount), //税价合计
                        workId: '', //	流程id
                    },
                })
                .then(() => {
                    showSuccessPop('保存成功！')
                    this.isSkip = false
                    this.$router.push('/materialsPurchasingClose')
                })
        },

        //保存按钮
        save () {
            let settlementDate = this.getTime(this.formData.settlementDate)
            this.$Ajax
                .httpPost({
                    url: '/materialsettle/purchaseSettlement/create',
                    params: {
                        acceptancePersonnelId: '', //	验收人员id
                        acceptancePersonnelName: this.formData.acceptancePersonnelName, //	验收人员名称
                        amount: parseInt(this.formData.amount), //	结算金额(元)
                        baseCurAmount: parseInt(this.formData.baseCurAmount), //	结算金额(本位币)
                        baseCurId: '', //	本位币id
                        baseCurName: this.formData.baseCurName, //本位币名称
                        baseCurRate: 0, //本位币汇率
                        baseCurTaxAmount: 0, //	税额(本位币)
                        baseCurTotalAmount: 0, //	税价合计(本位币)
                        billId: '', //	物资采购结算id
                        billNo: '', //	结算编号
                        borrowerId: '', //	借资人员id
                        borrowerName: this.formData.borrowerName, //	借资人员名称
                        easId: '', //	财务Id
                        founderId: '', //	创建人Id
                        founderName: '', //创建人名称
                        freight: parseInt(this.formData.freight), //	运输单位费用(含税,元)
                        freightDtls: [], //物资采购结算-运费明细集合
                        gmtCreate: '', //	创建时间
                        gmtModified: '', //更新时间
                        invoiceNo: this.formData.invoiceNo, //发票编号
                        isOffsetAdvancePayment: 0, //	是否冲预付款
                        isPersonalBorrowing: parseInt(this.formData.isPersonalBorrowing), //是否冲个人借资
                        materialDtls: [], //物资采购结算-物资明细集合
                        nullifyCreated: '', //	作废时间
                        nullifyCreator: '', //	作废人
                        nullifyCreatorId: '', //	作废人id
                        nullifyDescription: '', //	作废描述
                        nullifyReason: '', //	作废原因
                        offsetAdvancePayment: parseInt(this.formData.offsetAdvancePayment), //	冲预付款金额(元)
                        orgId: this.orgId, //	所属机构id
                        orgName: this.formData.orgName, //	所属机构名称
                        personalBorrowing: parseInt(this.formData.personalBorrowing), //	冲个人借资(元)
                        purchasingUnitId: '', //	采购单位id
                        purchasingUnitName: '', //	采购单位名称
                        remarks: this.formData.remarks, //备注信息
                        returnDepositDtls: [], //物资采购结算-保证及明细集合
                        reversalObjectId: '', //	冲销对象id
                        reversalObjectNumber: this.formData.reversalObjectNumber, //	冲销对象编号
                        rmbAmount: 0, //	结算金额(人民币
                        rmbId: '', //	人民币id
                        rmbName: '', //人民币名称
                        rmbRate: 0, //	人民币汇率
                        rmbTaxAmount: 0, //	税额(人民币)
                        rmbTotalAmount: 0, //	税价合计(人民币)
                        settlementDate: settlementDate, //	结算日期
                        settlementType: parseInt(this.formData.settlementType), //	结算类型
                        sourceBillId: '', //	源单id
                        sourceBillNo: this.formData.sourceBillNo, //	源单编号
                        state: '0', //	单据状态
                        supplierId: '', //	供应商id
                        supplierName: this.formData.supplierName, //	供应商名称
                        taxAmount: parseInt(this.formData.taxAmount), //	税额
                        taxDifference: parseInt(this.formData.taxDifference), //	税额差额
                        taxRate: parseInt(this.formData.taxRate), //税率
                        totalAmount: parseInt(this.formData.totalAmount), //税价合计
                        workId: '', //	流程id
                    },
                })
                .then(() => {
                    showSuccessPop('保存成功！')
                    this.isSkip = false

                })
        },
        //取消按钮
        cancel () {
            this.$router.push('/materialsPurchasingClose')
        },
        //日期转换格式
        getTime (dt) {
            var year = dt.getFullYear() //年
            var month = dt.getMonth() + 1 //月
            var date = dt.getDate() //日
            month = month < 10 ? '0' + month : month
            date = date < 10 ? '0' + date : date
            var str = year + '-' + month + '-' + date
            return str
        },
    },
}
</script>

<style lang="scss" scoped>
.buttons {
  left: 0;
}
.e-form {
  margin-bottom: 80px;
  padding: 0 20px;
  .tabs-title::before {
    content: "";
    height: 22px;
    width: 8px;
    border-radius: 40px;
    background-color: #2e61d7;
    display: block;
    position: absolute;
    left: 20px;
    margin-right: 20px;
  }
}

.e-table {
  background: #fff;
}

.el-tab-pane {
  margin-left: -10px;
}
</style>
